﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body id="acModel">
    <div id="msg"></div>
    <button data-bind="click:acSave" >提交</button>
    <button data-bind="click:acGet">提取</button>

    <script src="Scripts/jquery-1.11.3.min.js"></script>
    <script src="Scripts/knockout-3.3.0.js"></script>
    <script src="Scripts/knockout.validation.js"></script>
    <script type="text/javascript">
        var uri = "http://127.0.0.1:8089/api/books";
        //ssl
        //var uri = "https://192.168.1.48:8443/api/books";
        function acvm() {
            var self = this;
            self.Title = ko.observable().extend({ required: true, minLength: 3, maxLength: 64 });
            self.ReleaseDate = ko.observable().extend({ required: true });
            self.Genre = ko.observable().extend({ required: true, minLength: 3, maxLength: 64 });
            self.Price = ko.observable().extend({ required: true });

            var time = new Date();
            self.ReleaseDate(time.toUTCString());
            self.Title("titleweb");
            self.Genre("genreweb");
            self.Price(1000);

            self.getjson = function () {
                var book = new Object();
                book.Title = self.Title();
                book.Genre = self.Genre();
                book.Price = self.Price();
                book.ReleaseDate = self.ReleaseDate();
                return JSON.stringify(book);
            };

            self.acGet = function () {
                $.ajax({
                    type: "GET",
                    url: uri,
                    beforeSend: function (xhr) {
                        //xhr.setRequestHeader("U-ApiKey", "111"),
                        xhr.setRequestHeader("Content-Type", "application/json"),
                        xhr.setRequestHeader("Authorization", "Basic " + btoa("jac:jac"))
                    },
                    success: function (result) {
                        $("#msg").html(JSON.stringify(result));
                    },
                    error: function (xhr, status, error) {
                        $("#msg").html(xhr.status + " " + xhr.statusText);
                    }
                });
            }

            self.acSave = function () {
                if (self.isValid()) {
                    $.ajax({
                        type: "POST",
                        url: 'http://127.0.0.1:8080/api/users',
                        data: self.getjson(),
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("U-ApiKey", "aaaa"),
                            xhr.setRequestHeader("Content-Type", "application/json")
                        },
                        success: function (result) {
                            if (result === "") {
                                //$("#render").load("web/ac_uss_page.html");
                            } else {
                                $("#msg").html(result);
                                //$('#my-prompt').modal('open');
                            }
                        },
                        error: function (xhr, status, error) {
                            $("#msg").html(xhr.status + " " + xhr.statusText);
                            //$('#my-prompt').modal('open');
                        }
                    });
                } else {
                    $("#msg").html("数据验证失败！");
                    //$('#my-prompt').modal('open');
                }
            };

            //self.cancel = function () {
            //    self.userId("");
            //    self.pwd("");
            //    self.userName("");
            //    self.email("");
            //    self.qq("");
            //};

        }
        var dvvm = new acvm();
        ko.applyBindings(ko.validatedObservable(dvvm), document.getElementById("acModel"));
    </script>
</body>
</html>
